package com.tsy.graduate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Strings;
import com.tsy.graduate.bean.User;
import com.tsy.graduate.constant.WxAppConstant;
import com.tsy.graduate.httpdto.ObjectDataResponse;
import com.tsy.graduate.service.UserService;
import com.tsy.graduate.exception.BusinessException;
import com.tsy.graduate.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.BagUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.awt.image.BufferStrategy;
import java.util.List;

@Api("登陆接口")
@RestController
@RequestMapping("/tsy")
public class LoginController {
    @Autowired
    UserService userService;

    @ApiOperation("登陆")
    @GetMapping("/login")
    public ObjectDataResponse<User> login(@RequestParam() String username,
                                          @RequestParam() String pwd,
                                          @RequestParam() Byte type) {
        if (type == null || Strings.isNullOrEmpty(username) || Strings.isNullOrEmpty(pwd)) {
            throw new BusinessException(10001, "关键参数不全");
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(User::getUsername, username);
        User user = userService.getOne(queryWrapper);
        if (!user.getPwd().equals(pwd)) {
            throw new BusinessException(401, "密码不匹配");
        }
        if (!user.getType().equals(type)) {
            throw new BusinessException(401, "登陆用户类型不匹配");
        }
        user.setPwd(null);
        return new ObjectDataResponse<>(user);
    }

    @GetMapping("/signUp")
    @ApiOperation("注册接口")
    public ObjectDataResponse<Boolean> addUser(@RequestParam() String username,
                                               @RequestParam() String pwd,
                                               @RequestParam() String name,
                                               @RequestParam(defaultValue = "1", required = false) Byte type
    ) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if (Strings.isNullOrEmpty(username) || Strings.isNullOrEmpty(pwd) || Strings.isNullOrEmpty(name)) {
            throw new BusinessException(1001, "关键参数不全");

        }
        queryWrapper.lambda().eq(User::getUsername, username);
        List<User> userList = userService.list(queryWrapper);
        if (userList.size() > 0) {
            //todo  抛出已经存在用户名异常
            throw new BusinessException(1000, "已经存在此账号");
        }
        if (!type.equals(WxAppConstant.USER_TYPE_STUDENT) && !type.equals(WxAppConstant.USER_TYPE_TEACHER)) {
            throw new BusinessException(2002, "type必须是1或2");
        }
        User user = new User();
        int timestamp = DateUtils.getCurrentTimestamp();

        user.setUsername(username)
                .setName(name)
                .setType(type)
                .setUpdated(timestamp)
                .setCreated(timestamp)
                .setPwd(pwd);
        userService.save(user);
        return new ObjectDataResponse<>(true);
    }


}
